// get popup2content info
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  console.log('request :>> ', request);
  console.log(request.action)
  // sendResponse('我收到了你的情书，popup~')
  if (request.action == 'current') {
    const title = document.title
    const url = window.location.href
    const mdUrl = `[${title}](${url})`
    console.log('mdUrl :>> ', mdUrl);
    copyTextToClipboard(mdUrl)
  } else if (request.action == 'all') {
    chrome.runtime.sendMessage({action: "getTabs"}, function(response) {
      console.log(response.tabInfo);
      const mdAllUrl = response.tabInfo.map(item => `[${item.title}](${item.url})`).join('\n')
      copyTextToClipboard(mdAllUrl)
    });
  }
})
function copyTextToClipboard(text) {
  const tempTextArea = document.createElement('textarea');
  tempTextArea.style.position = 'absolute';
  tempTextArea.style.left = '-1000px';
  tempTextArea.style.top = '-1000px';
  tempTextArea.value = text;
  document.body.appendChild(tempTextArea);
  tempTextArea.select();
  document.execCommand('copy');
  document.body.removeChild(tempTextArea);
}